﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;

using System.ComponentModel;
using System.Data;

using System.Windows.Forms;
using Vux.Helper.Imaging;
using Vux.Helper.File;
//using BigInteger;
namespace svCore.App
{
    public delegate void UpdateProfilePictureHandler(byte[] ip_bmp_data);

    public class UserProfile
    {
        #region Private Members
        /// <summary>
        /// Bảng mã hiệu chứa các thông tin người dùng.
        /// 1. Ảnh cá nhân (
        /// </summary>
        private Hashtable m_ht_tokens;

        private byte[] m_img_data;

        private string m_str_usr_name;
        private string m_str_profile_url;

        #endregion

        public string UserName
        {
            get { return m_str_usr_name; }
            set { m_str_usr_name = string.IsNullOrEmpty(value) == true ? "manhsu3110c" : value; }
        }

        public event UpdateProfilePictureHandler UpdateProfilePicture;

        protected virtual void OnUpdateProfilePicture(byte[] ip_bmp_data)
        {
            if (UpdateProfilePicture != null)
            {
                UpdateProfilePicture(ip_bmp_data);
            }
        }

        public bool HasProfile
        {
            get { return m_ht_tokens["IMG"] != null; }
        }

        public Stream GetPictureStream()
        {
            return new MemoryStream(m_img_data);
        }

        public byte[] GetPictureData()
        {
            return m_img_data;
        }

        public void SaveUserPicture(string ip_str_path)
        {
            SaveUserProfile(ip_str_path);
            OnUpdateProfilePicture(m_img_data);
        }

        public string PictureName
        {
            get { return m_ht_tokens["IMG"] as string; }
        }

        public UserProfile()
        {
            m_ht_tokens = new Hashtable();
            // Luôn có: MAC Address
            m_ht_tokens["MA"] = "Yes";
            m_str_profile_url = "Profile.Vux";
            LoadUserProfile();
        }

        public UserProfile(string ip_str_profile)
        {
            m_ht_tokens = new Hashtable();
            m_ht_tokens["MA"] = "Yes";
            m_str_profile_url = ip_str_profile;
            LoadUserProfile();
        }

        public string this[string ip_str_key]
        {
            get { return m_ht_tokens[ip_str_key] as string; }
            set { m_ht_tokens[ip_str_key] = value; }
        }

        public void AddTokens(string ip_str_token, string ip_str_data)
        {
            m_ht_tokens[ip_str_token] = ip_str_data;
        }

        public void DelTokens(string ip_str_token)
        {
            m_ht_tokens[ip_str_token] = null;
        }
        /// <summary>
        ///  Impress confidential information
        /// </summary>
        private void TumbleImage(string ip_bmp_path)
        {
            if (Directory.Exists("Images") == false)
            {
                Directory.CreateDirectory("Images");
            }

            var v_str_save = Path.GetFileNameWithoutExtension(ip_bmp_path);
            var v_str_fname = string.Format(@"Images\{0}.bmp", "Default"); // v_str_save);
            if (File.Exists(ip_bmp_path) == false)
            {
                throw new Exception("File name not exits: " + v_str_save);
            }
            var v_buffer = FileReader.ReadAllBytes(ip_bmp_path);
            var v_stream = new MemoryStream(v_buffer);
            var v_bm_image = new Bitmap(ip_bmp_path);
            var v_img_processing = new CImageProcessing();
            var v_str_mac = string.IsNullOrEmpty(m_str_usr_name) == true ? "manhsu3110c" : m_str_usr_name;
            v_bm_image = v_img_processing.HamGiauTin(v_bm_image, v_str_mac, "Images");
            v_bm_image.Save(v_str_fname);
            // lưu địa chỉ profile picture
            m_ht_tokens["IMG"] = v_str_fname;
            m_ht_tokens["USR"] = v_str_mac;
            m_img_data = FileReader.ReadAllBytes(v_str_fname);
        }

        private void SaveUserProfile(string ip_str_path)
        {
            TumbleImage(ip_str_path);
            // Ghi lại file cấu hình
            var v_fstream = new FileStream(m_str_profile_url, FileMode.Create, FileAccess.Write, FileShare.Read);
            var v_fwriter = new StreamWriter(v_fstream);

            foreach (string v_str_key in m_ht_tokens.Keys)
            {
                var v_str_token = string.Format("{0}|{1}", v_str_key, m_ht_tokens[v_str_key]);
                v_fwriter.WriteLine(v_str_token);
            }
            v_fwriter.Close();
            v_fstream.Close();
        }

        private void LoadUserProfile()
        {
            try
            {
                if (File.Exists(m_str_profile_url) == true)
                {
                    var v_fstream = new FileStream(m_str_profile_url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    var v_freader = new StreamReader(v_fstream);
                    var v_str_token = string.Empty;
                    while ((v_str_token = v_freader.ReadLine()) != null)
                    {
                        var v_str_tokens = v_str_token.Split('|');
                        AddTokens(v_str_tokens[0], v_str_tokens[1]);
                    }
                    v_freader.Close();
                    v_fstream.Close();
                }
                m_str_usr_name = m_ht_tokens["USR"] as string;
                if (m_ht_tokens["IMG"] != null && File.Exists((string)m_ht_tokens["IMG"]) != false)
                {
                    m_img_data = FileReader.ReadAllBytes((string)m_ht_tokens["IMG"]);
                }
                else
                {
                    m_img_data = null;
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
